import Vue from "vue";
import Router from "vue-router";
import Home from "../components/Home.vue";
import Home2 from "../components/Home2.vue";
import userInfo from "../components/userInfo";
import Login from "@/views/Login.vue";
import Login2 from "@/views/Login2.vue";
import Login3 from "@/views/Login3.vue";
import helpCenter from "../components/help-center.vue";
import newsCenter from "../components/news-center.vue";
import Opinions from "../components/Opinions";
// 服务指南
import serviceGuide from "../components/service-guide.vue";
// 服务指南--二级
import serviceList from "../components/service-guide-list.vue";
// 服务指南--详情
import serviceDetails from "../components/service-guide-details.vue";

import address from "../components/address.vue"

import Resources from "../components/Resources.vue"

import errorIE from "../components/errorIE.vue"

Vue.use(Router);

const routes = [
  {
    path: "/",
    name: "Login3",
    component: Login3
  },
  {
    path: "/Login2",
    name: "Login2",
    component: Login2
  },
  {
    path: "/Login",
    name: "Login3",
    component: Login3
  },
  {
    path: "/userInfo",
    name: "userInfo",
    component: userInfo,
    meta: { islogin: true }
  },
  {
    path: "/home",
    name: "home",
    component: Home,
    meta: { islogin: true }
  },
  {
    path: "/home2",
    name: "home2",
    component: Home2,
    meta: { islogin: true }
  },
  {
    path: "/helpcenter",
    name: "helpCenter",
    component: helpCenter,
    meta: { islogin: true }
  },
  {
    path: "/newsCenter",
    name: "newsCenter",
    component: newsCenter,

    meta: { islogin: true }
  },
  {
    path: "/Opinions",
    name: "Opinions",
    component: Opinions,
    meta: { islogin: true }
  },
  {
    path: "/serviceGuide",
    name: "serviceGuide",
    component: serviceGuide,
    meta: { islogin: true }
  },
  {
    path: "/serviceList",
    name: "serviceList",
    component: serviceList,
    meta: { islogin: true }
  },
  {
    path: "/serviceDetails",
    name: "serviceDetails",
    component: serviceDetails,
    meta: { islogin: true }
  },
  {
    path: "/errorIE",
    name: "errorIE",
    component: errorIE,
    meta: { islogin: false }
  },{
    path: "/address",
    name: "address",
    component: address,
    meta: { islogin: false }
  },{
    path: "/Resources",
    name: "Resources",
    component: Resources,
    meta: {islogin: false }
  }
];

const router = new Router({
  mode: "hash",
  base: "/",
  routes: routes
});

// 登录拦截
router.beforeEach((to, from, next) => {
  // console.log(to);

  let islogin = localStorage.getItem("TOKEN");

  // let islogin = true

  // if (to.name == null) {
  //   //页面不存在，跳转404

  //   next('/404')

  //   return
  // }

  // 需要登陆
  if (to.meta.islogin) {
    if (islogin) {
      // 已经登陆

      next();
    } else {
      // 未登陆
      next("/");
    }
  } else {
    next();
  }
});

export default router;
